package com.contest.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.contest.entity.RoomInfo;
import com.contest.util.IdWorker;
import com.contest.util.PageUtils;
import com.contest.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.*;

import com.contest.entity.SessionInfo;
import com.contest.service.SessionInfoService;

import javax.servlet.http.HttpServletRequest;


/**
 * 
 *
 * @author yfl
 * @email 617823298@qq.com
 * @date 2018-04-28 16:55:47
 */

@Controller
@RequestMapping("/sessionInfo")
public class SessionInfoController {

    @Autowired
    SessionInfoService sessionInfoService;
    @Autowired
    IdWorker idWorker;

    @RequestMapping("/client/getSessionInfosByRoom")
    @ResponseBody
    public Map getSessionInfosByRoom(HttpServletRequest request) {
        Map msg = new HashMap();
        sessionInfoService.getSessionInfosByRoom(request , msg);
        return msg;
    }
    @RequestMapping("/client/getSessionInfosBySession")
    @ResponseBody
    public Map getSessionInfosBySession(HttpServletRequest request) {
        Map msg = new HashMap();
        sessionInfoService.getSessionInfosBySession(request , msg);
        return msg;
    }


    @RequestMapping("/list")
    @ResponseBody
    public PageUtils list(@RequestParam Map<String,Object> param) {
        int page = Integer.parseInt(param.get("page").toString());
        int rows = Integer.parseInt(param.get("rows").toString());
        Page<SessionInfo> mPage = new Page<SessionInfo>(page, rows);
        param.remove("page");
        param.remove("rows");
        Wrapper<SessionInfo> w = new EntityWrapper<>();
        Set<String> keys = param.keySet();
        for (String key : keys) {
            Object value = param.get(key);
            if (value != null && !"".equals(value)) {
                w.like(key, value.toString());
            }
        }
        mPage = sessionInfoService.selectPage(mPage , w);
        return new PageUtils(mPage.getRecords(),mPage.getTotal());
    }

    @PostMapping("/save")
    @ResponseBody
    public R save(SessionInfo sessionInfo) {
        sessionInfo.setFdId(idWorker.nextId() + "");
        if (sessionInfoService.insert(sessionInfo)) {
            return R.ok("操作成功!");
        } else {
            return R.error("操作失败");
        }
    }

    @GetMapping("/edit/{fdId}")
    public String edit(Model model, @PathVariable("fdId") String fdId) {
        SessionInfo sessionInfo = sessionInfoService.selectById(fdId);
        model.addAttribute("sessionInfo", sessionInfo);
        return "sessionInfo/edit";
    }

    @PostMapping("/remove")
    @ResponseBody
    public R remove(@RequestParam("ids[]") String[] ids) {
        List<String> fdIds = new ArrayList<String>();
        for (String id : ids) {
            fdIds.add(id);
        }
        if (sessionInfoService.deleteBatchIds(fdIds)) {
            return R.ok("删除成功");
        } else {
            return R.error(-1, "删除失败!");
        }
    }

    @PostMapping("/update")
    @ResponseBody
    public R update(SessionInfo sessionInfo) {
        if (sessionInfoService.updateResult(sessionInfo)) {
            return R.ok("更新成功!");
        } else {
            return R.error(-1, "更新失败!");
        }
    }

}
